home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 23 / Amiga Format AFCD23 (Feb 1998, Issue 107).iso / -seriously_amiga- / shareware / hardware / truesound / source / players / playmp2.c next >
C/C++ Source or Header  |  1997-12-01  |  4KB  |  196 lines

  1. /*
  2. Example mpeg layer2 player for truesound.device
  3. By Pontus Fuchs 1997
  4. */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <exec/exec.h>
  9. #include <dos/dos.h>
  10.  
  11. #include <proto/dos.h>
  12. #include <proto/exec.h>
  13.  
  14. #include <devices/truesound.h>
  15.  
  16. #define BUFFSIZE 0x10000 /*Must be even*/
  17.  
  18. /*protos*/
  19. int startup(void);
  20. void closedown(void);
  21. int getargs(void);
  22. void mainloop(void);
  23. ULONG fillbuffer(struct IOStdReq *ioreq, char *buffer);
  24.  
  25. /*Globala variabler*/
  26. char *filename=0;
  27. struct RDArgs *args=0;
  28. struct MsgPort *msgport1;
  29. struct MsgPort *msgport2;
  30. struct IOStdReq *ioreq1;
  31. struct IOStdReq *ioreq2;
  32. int deviceopen=0;
  33. BPTR file=0;
  34.  
  35. char buffer1[BUFFSIZE];
  36. char buffer2[BUFFSIZE];
  37.  
  38. void main(void)
  39. {
  40.   if(startup())
  41.   {
  42.     printf("Example MPEG layer2-player for truesound.device\nCopyright © 1997 by Pontus Fuchs\n");
  43.     printf("Now playing: %s\n", filename);
  44.     mainloop();
  45.     printf("\n");
  46.   }
  47.   closedown();
  48. }
  49.  
  50. void mainloop(void)
  51. {
  52. ULONG sigrcvd;
  53. int done=1;
  54. ULONG buffsize1;
  55. ULONG buffsize2;
  56.  
  57.   ioreq1->io_Command=L3CMD_GOMP2; /*Load l2-codec*/
  58.   DoIO((struct IORequest*) ioreq1);
  59.  
  60.   buffsize1=fillbuffer(ioreq1, buffer1);
  61.   buffsize2=fillbuffer(ioreq2, buffer2);
  62.  
  63.   while(done)
  64.   {
  65.     sigrcvd=Wait(SIGBREAKF_CTRL_C|1<<msgport1->mp_SigBit|1<<msgport2->mp_SigBit);
  66.  
  67.     if(sigrcvd & SIGBREAKF_CTRL_C) /* ctrl_c? */
  68.       done=0;
  69.  
  70.     if(sigrcvd & (1<<msgport1->mp_SigBit))
  71.     {
  72.       WaitIO( (struct IORequest*) ioreq1);
  73.       buffsize1=fillbuffer(ioreq1, buffer1);
  74.       /*printf("Filled buffer1 with %d bytes\n", buffsize1);*/
  75.  
  76.     }
  77.  
  78.     if(sigrcvd & (1<<msgport2->mp_SigBit))
  79.     {
  80.       WaitIO( (struct IORequest*) ioreq2);
  81.       buffsize2=fillbuffer(ioreq2, buffer2);
  82.       /*printf("Filled buffer2 with %d bytes\n", buffsize2);*/
  83.     }
  84.  
  85.     if((buffsize1==0) & (buffsize2==0))  /*exit if both buffers are empty*/
  86.       done=0;
  87.  
  88.   }
  89.  
  90. /*Abort any active IO-requests*/
  91.   if(buffsize1)
  92.   {
  93.     AbortIO( (struct IORequest*) ioreq1);
  94.     WaitIO( (struct IORequest*) ioreq1);
  95.   }
  96.  
  97.   if(buffsize2)
  98.   {
  99.     AbortIO( (struct IORequest*) ioreq2);
  100.     WaitIO( (struct IORequest*) ioreq2);
  101.   }
  102.  }
  103.  
  104. ULONG fillbuffer(struct IOStdReq *ioreq, char *buffer)
  105. {
  106. ULONG readsize;
  107.  
  108.   readsize=Read(file, buffer, BUFFSIZE);
  109.   if(readsize==-1)
  110.   {
  111.     printf("Read error!\n");
  112.     return 0;
  113.   }
  114.  
  115.   if(readsize)
  116.   {
  117.     ioreq->io_Command=CMD_WRITE;
  118.     ioreq->io_Data=buffer;
  119.     ioreq->io_Length=readsize;
  120.     SendIO((struct IORequest*) ioreq);
  121.   }
  122.  
  123.   return readsize;
  124. }
  125.  
  126. int startup(void)
  127. {
  128.   if(!getargs())
  129.     return 0;
  130.  
  131.   if((msgport1=CreateMsgPort())==0)
  132.     return 0;
  133.   if((ioreq1=CreateIORequest(msgport1, sizeof(struct IOStdReq)))==0)
  134.     return 0;
  135.   if(OpenDevice("truesound.device", 0, (struct IORequest*) ioreq1, 0))
  136.     return 0;
  137.   deviceopen++;
  138.  
  139.   if((msgport2=CreateMsgPort())==0)
  140.     return 0;
  141.   if((ioreq2=CreateIORequest(msgport2, sizeof(struct IOStdReq)))==0)
  142.     return 0;
  143.   if(OpenDevice("truesound.device", 0, (struct IORequest*) ioreq2, 0))
  144.     return 0;
  145.   deviceopen++;
  146.  
  147.   if((file=Open(filename, MODE_OLDFILE))==0)
  148.   {
  149.     printf("Error: Could't open %s\n",filename);
  150.     return 0;
  151.   }
  152.  
  153.   return 1;
  154. }
  155.  
  156. void closedown(void)
  157. {
  158.   if(file)
  159.     Close(file);
  160.  
  161.   if(deviceopen) /*Check if IOreq1 was opened*/
  162.     {
  163.       CloseDevice((struct IORequest*) ioreq1);
  164.       deviceopen--;
  165.     }
  166.  
  167.   if(deviceopen) /*Check if IOreq2 was opened*/
  168.     CloseDevice((struct IORequest*) ioreq2);
  169.  
  170.   DeleteIORequest(ioreq1); /*passing NULL is safe*/
  171.   DeleteMsgPort(msgport1);
  172.   DeleteIORequest(ioreq2);
  173.   DeleteMsgPort(msgport2);
  174.  
  175.   FreeArgs(args);
  176. }
  177.  
  178. int getargs(void)
  179. {
  180. LONG array[2]={0,};
  181.  
  182.   if(args=ReadArgs("FILE/A",array,NULL))
  183.   {
  184.     if(array[0])
  185.       filename=(char*)array[0];
  186.   }
  187.  
  188.   if(filename==0)
  189.   {
  190.     printf("Play WHAT?\n");
  191.     return 0;
  192.   }
  193.   else
  194.     return 1;
  195. }
  196.